package br.com.yurikimo.ykkit;

import android.webkit.WebView;
import android.widget.RelativeLayout;

public class YKWebView extends WebView implements YKBaseComponent{
	private RelativeLayout.LayoutParams layoutParams;
	private YKContainer parent = null;
	private float x = 0;
	private float y = 0;
	private float width = 100;
	private float height = 50;
	private RelativeLayout relativeLayout = null;

	public YKWebView(){
		super(YKDirector.getActivity());
		
		getSettings().setJavaScriptEnabled(true);
		getSettings().setPluginsEnabled(true);
//		getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
	}
	
	public void setBounds(float x, float y, float width, float height){
		layoutParams = null;
		
		layoutParams = new RelativeLayout.LayoutParams((int)width, (int)height);
		
		layoutParams.leftMargin = (int)x;
		layoutParams.topMargin = (int)y;		
		
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
	}
	
	public void addToLayout(RelativeLayout relativeLayout){
		this.relativeLayout = relativeLayout;
		
		relativeLayout.addView(this, layoutParams);
		relativeLayout.invalidate();
	}
	
	public float getObjectWidth(){
		return width;
	}
	
	public float getObjectHeight(){
		return height;
	}

	public float getObjectX(){
		return x;
	}
	
	public float getObjectY(){
		return y;
	}	
	
	public void refreshLayout(){
		setBounds(x, y, width, height);
		
		if (relativeLayout != null){
			if (relativeLayout.indexOfChild(this) >= 0){
				relativeLayout.removeView(this);
				
				relativeLayout.addView(this, layoutParams);
				relativeLayout.invalidate();
			}
		}
	}
	
	public void setPosition(float x, float y){
		this.x = x;
		this.y = y;
	
		refreshLayout();
	}

	public RelativeLayout getLayoutView()
	{
		if (relativeLayout == null){
			relativeLayout = new RelativeLayout(getContext());
			relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
		}
		
		return relativeLayout;
	}

	public YKContainer getObjectParent()
	{
		return parent;
	}

	public void removeFromSuperView()
	{
		relativeLayout.removeView(this);
	}

	public void setObjectParent(YKContainer parent)
	{
		this.parent = parent;
	}
}
